package com.bjpowernode.config.handler;

import com.bjpowernode.result.R;
import com.bjpowernode.util.ResponseUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 登录失败调用该handler
 *
 */
@Component
public class DlykAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8");

        //登录失败后，返回一个“统一的结果对象”
        R result = R.FAIL(exception.getMessage());

        //把“统一的结果对象”R写出到前端
        ResponseUtil.write(response, result);
    }
}
